package com.zegome.game.gomestory.main.libs;

import java.util.Vector;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;
import com.zegome.game.gomestory.main.Center;

public class AutoRunText extends ScrollPane{

	private int x,y,wi,hi;
	private Label label;	
	public Image image;
	public ImageButton button;
	private String link;
	
	
	public Vector<NotifyItem> notify;
	private String text;
	private String imgPath;
	private float timeWaitNotify;
	private float timeWaitRunAgain;
	private int stateNotify;
	private int scrollX;
	private BitmapFont font;	
	private int cursor;
	Center ctr;
	
	Texture icon;
	
	// ===========================================================
	// Constants
	// ===========================================================
	
	// ===========================================================
	// Fields
	// ===========================================================

	// ===========================================================
	// Constructor
	// ===========================================================

	public AutoRunText(Center ctr, Actor widget) {
		super(widget);
		this.ctr = ctr;
		// TODO Auto-generated constructor stub
		notify = new Vector<NotifyItem>();
		cursor = 0;
		image = new Image();
	}
	
	public void init(){
		if(button == null){			
			button = ctr.stageHelper.createImageButton(ctr.g.mSkin, ctr.a.transRegion, null, null, x, y,600,40);
			button.addListener(new ChangeListener() {
				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					// TODO Auto-generated method stub
					if(link!=null){
						ctr.androidHelper.openlink(link);
					}
				}
			});
		}
	}
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public void setXY(int x, int y){		
		this.x = x;
		this.y = y;
	}
	public void setWH(int wi, int hi){		
		this.wi = wi;
		this.hi = hi;				
	}
	public void setBitmapFont(BitmapFont font){
		this.font = font;
	}
	public void addNotify(int type, String content, String image, String link){		
		notify.add(new NotifyItem(content, image, link));		
		if(ctr.d.isNetworkAvaiable){
			if(type>=0){		
				ctr.d.saveNotify(type, content, link, image);				
			}
		}
		else{
			ctr.androidHelper.log("ADD NOTIFY OFFLINE "+ content +" -- "+link+" -- "+image);
		}
	}		
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	@Override
	public void act(float pDeltaTime) {		
		// TODO Auto-generated method stub		
		
		if (text == null) {
			if (notify.size() > 0) {
				if(cursor > notify.size()-1)
				{					
					stateNotify = 5;
					timeWaitRunAgain = 0;
					cursor = 0;
					return;
				}
				text = "                                                                 "
						+ notify.elementAt(cursor).content+"            ";
				imgPath = notify.elementAt(cursor).image;
				link = notify.elementAt(cursor).link;
				scrollX = 0;
				cursor ++;
			}
			else{
				this.setVisible(false);
			}
		}
		
		if(stateNotify == 5){
			if(timeWaitRunAgain > 5){
				stateNotify = 0;
			}
			else{
				timeWaitRunAgain+=pDeltaTime;				
			}
			this.setVisible(false);
		}

		if (stateNotify == 0) {
			if (text != null) {
				setTextNotify(text);				
			}
		}
		
		if(this.isVisible() == true)
		{
			if(this.getWidget().getWidth()>5){
				if(stateNotify == 2){					
							
					if(scrollX < this.getMaxX())
					{		
						this.setScrollX(scrollX);									
						scrollX+=1*(((pDeltaTime/0.02f)<1)?1:(pDeltaTime/0.02f));
					}
					else
					{
						if(timeWaitNotify>1){							
							stateNotify = 3;
						}
						else{
							timeWaitNotify += pDeltaTime;
						}
					}
				}
				else if(stateNotify == 3){	
					this.setVisible(false);
					image.setVisible(false);
					button.setVisible(false);
					stateNotify = 0;
					timeWaitNotify = 0;											
					text = null;
					scrollX = 0;									
				}
			}
		}		
		super.act(pDeltaTime);
	}
	// ===========================================================
	// Methods
	// ===========================================================
	private void setTextNotify(String text){
		if(label == null){
			LabelStyle style = new LabelStyle(font, Color.WHITE);
			label = new Label("", style);
			setWidget(label);
		}
		if(imgPath != null){
			if(icon !=null){
				icon.dispose();
			}
			icon = new Texture(Gdx.files.external(imgPath));
			icon.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			TextureRegion iconRg = new TextureRegion(icon);
			if(image == null){
				//image = ctr.stageHelper.createImage(icon, x, y, 40, 40);							
			}
			else{
				image.setDrawable(new TextureRegionDrawable(iconRg));
				image.setPosition(x+600-64, y);
				image.setSize(64, 64);
			}
			image.setVisible(true);
			button.setVisible(true);
		}
		else{
			if(image!=null){
				image.setVisible(false);
			}
			button.setVisible(false);
		}
		label.setText(text);		
		this.setBounds((x), (y), (wi), (hi));
		this.setSmoothScrolling(false);
		this.setScrollingDisabled(false, true);
						
		this.setY(y);
		this.setScrollX(0);
		timeWaitNotify = 0;
		stateNotify = 2;
		this.setVisible(true);
	}
	// ===========================================================
	// Inner and Anonymous Classes
	// ==========================================================
}
